package com.example.springsecuritydemo;

import com.example.springsecuritydemo.restful.Employee;
import com.example.springsecuritydemo.restful.EmployeeRepository;
import com.example.springsecuritydemo.test.Inter;
import com.example.springsecuritydemo.test.Quote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;

@SpringBootApplication
//@EnableScheduling
public class SpringsecuritydemoApplication {
    private final Inter interImpl2;

    private static final Logger log = LoggerFactory.getLogger(SpringsecuritydemoApplication.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    public SpringsecuritydemoApplication(Inter interImpl2) {
        this.interImpl2 = interImpl2;
    }

    public static void main(String... args) {
        SpringApplication.run(SpringsecuritydemoApplication.class, args);
    }
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));
    }

    @Bean
    public CommandLineRunner run(RestTemplate restTemplate, EmployeeRepository repository) {
        return args -> {
            Quote quote = restTemplate.getForObject(
                    "https://gturnquist-quoters.cfapps.io/api/random", Quote.class);
            assert quote != null;
            log.info(quote.toString());
            interImpl2.a();
            log.info("Preloading " + repository.save(new Employee("Bilbo Baggins", "burglar")));
            log.info("Preloading " + repository.save(new Employee("Frodo Baggins", "thief")));
        };
    }
}
